clear all;
clc;

tic
disp('Loading data...');
load 7nn_vv;
load mnist_test;
load mnist_train;
V = VV;
clear VV;

s = 100;

l = size(yL,2);

C = 1;
c = 10;

Y = yL';

disp('Data loaded!');

n = size(V,1);

nump = c*(c-1)/2;
sz = zeros(c,1);
idx = cell(10,1);
for i = 1:c
    idx{i}=find(Y==(i-1));
    sz(i)=size(idx{i},1);
end

ix = cell(c,c);
Y = cell(c,c);
P = cell(c,c);

for i = 1:c
    for j = i+1:c
        ix{i,j} = [idx{i};idx{j}];
        Y{i,j} = [ones(sz(i),1);-ones(sz(j),1)];
        P{i,j} = repmat(Y{i,j},1,s).*V(ix{i,j},1:s);
    end
end

Q = V(l+1:n,:);

X = [xL,xU;ones(1,n)];
T = X*V(:,1:s);
TT = T'*inv(X*X')*T;
clear xL;
clear xU;
clear X;
clear T;

disp('Start optimization...');
tic

ct = 1;
cvx_begin sdp
cvx_solver sedumi
cvx_solver_settings('maxiter',500);
    variable t
    variable gam(s,nump)
    variable M(s,s) symmetric
    variable del(l,1) nonnegative
    variable bb(nump,1)
    minimize (t+2*C*sum(del));
    for i = 1:c
        for j = i+1:c
            [M gam(:,ct);gam(:,ct)' t] >= 0;
            P{i,j}*gam(:,ct)+bb(ct)*Y{i,j}-ones(size(P{i,j},1),1)+del(ix{i,j}) >= 0;
            ct=ct+1;
        end
    end
    trace(M) - trace(M*TT) == n;
cvx_end
optime = toc

% gam = zeros(s,nump);
% del = zeros(l,1);
% bb = zeros(nump,1);

Yp = zeros(n-l,1);

VM = V(:,1:s)*gam+repmat(bb',n,1);

for x = 1:n
    vt = zeros(c,1);
    ct = 1;
    for i = 1:c
        for j = i+1:c
            if (VM(x,ct) > 0)
                vt(i)=vt(i)+1;
            else vt(j)=vt(j)+1;
            end
            ct=ct+1;
        end
    end
    [~,Yp(x)] = max(vt);
end

Yp = Yp-1;
trerr = calerr(Yp(1:l),yL')
tserr = calerr(Yp(l+1:n),yU')
numsv = sum(del>0)
save('res/mlt_pair','trerr','tserr','numsv');
